package han.study.connection;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {
    @Test
    public void test01() throws SQLException {
        Driver driver = new com.mysql.cj.jdbc.Driver();
        String url = "jdbc:mysql://localhost:3306/myemployees";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");
        Connection conn = driver.connect(url,info);
        System.out.println(conn);
        conn.close();
    }

    @Test
    public void test02() throws SQLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        // 通过反射获取数据库驱动
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();
        // 提供要连接的数据库
        final String url = "jdbc:mysql://localhost:3306/myemployees";
        // 提供数据用户名和密码
        final Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");
        // 获取连接
        Connection conn = driver.connect(url,info);
        System.out.println(conn);
        conn.close();
    }

    // 方式三 使用 DriverManager 做数据连接
    @Test
    public void test03() throws ClassNotFoundException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        // 注册驱动
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();
        DriverManager.registerDriver(driver);
        // 提供另外三个连接的信息
        final String url = "jdbc:mysql://localhost:3306/myemployees";
        final String user = "root";
        final String password = "123456";
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
        conn.close();
    }

    @Test
    public void test04() throws Exception{
        // 提供另外三个连接的信息
        final String url = "jdbc:mysql://localhost:3306/myemployees";
        final String user = "root";
        final String password = "123456";
        // 加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //Driver driver = (Driver) clazz.getDeclaredConstructor().newInstance();
        // 注册驱动
        //DriverManager.registerDriver(driver);
        // 获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
        conn.close();
    }

    @Test
    public void test05() throws Exception{
        final String url = "jdbc:mysql://localhost:3306/myemployees";
        final String user = "root";
        final String password = "123456";
        // 这个地方省略类 class.forName 这个一般不可省略，mysql可以省略但是对于其他数据库就不好使了
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
        conn.close();
    }
    // 最终版本的获取连接的方式,将数据库需要的四个基本信息声明在配置文件中
    // 通过读取配置文件的方式来获取连接
    // 实现了数据和代码的分离
    // 通过配置文件，数据修改后避免了重复打包
    @Test
    public void getConnection() throws Exception {
        // 通过properties获取属性
        InputStream in = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties props = new Properties();
        props.load(in);
        String user = props.getProperty("user");
        String password = props.getProperty("password");
        String url = props.getProperty("url");
        String driverClass = props.getProperty("driverClass");
        // 加载驱动
        Class.forName(driverClass);
        // 获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println(conn);
        conn.close();
    }
}
